<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    :fullscreen="true">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="文章类型" prop="articleType" hidden>
        <el-input v-model="dataForm.articleType" placeholder="文章类型"></el-input>
      </el-form-item>
      <el-form-item label="文章题目" prop="articleTitle">
        <el-input v-model="dataForm.articleTitle" placeholder="文章题目"></el-input>
      </el-form-item>
      <el-form-item label="文章内容" prop="articleContent">
        <tinymce v-if="this.isRouterAlive" v-model="dataForm.articleContent" ref="editor"></tinymce>
      </el-form-item>
      <el-form-item label="是否删除" prop="isDelete" hidden>
        <el-input v-model="dataForm.isDelete" placeholder="是否删除"></el-input>
      </el-form-item>
      <el-form-item label="县" prop="county" hidden>
        <el-input v-model="dataForm.county" placeholder="县"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="this.isLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import Tinymce from '@/components/Tinymce'

  export default {
    data () {
      return {
        visible: false,
        isRouterAlive: true,
        isLoading: false,
        dataForm: {
          id: 0,
          createUserId: this.$store.state.user.id,
          createUserName: this.$store.state.user.name,
          articleType: 3,
          articleTitle: '',
          createTime: '',
          updateTime: '',
          articleContent: '',
          isDelete: 0,
          county: ''
        },
        dataRule: {
          articleTitle: [
            {required: true, message: '题目不能为空', trigger: 'blur'}
          ],
          articleContent: [
            {required: true, message: '文章具体内容不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    components: {
      Tinymce
    },
    computed: {
      userId: {
        get () {
          return this.$store.state.user.id
        }
      }
    },
    methods: {
      reload () {
        this.isRouterAlive = false
        this.$nextTick(() => (this.isRouterAlive = true))
      },
      init (id) {
        this.dataForm.id = id || 0
        this.isLoading = false
        this.reload()
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/basisdata/basisdata/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createUserId = data.basisData.createUserId
                this.dataForm.createUserName = data.basisData.createUserName
                this.dataForm.articleType = data.basisData.articleType
                this.dataForm.articleTitle = data.basisData.articleTitle
                this.dataForm.createTime = data.basisData.createTime
                this.dataForm.updateTime = data.basisData.updateTime
                this.dataForm.articleContent = data.basisData.articleContent
                this.dataForm.isDelete = data.basisData.isDelete
                this.dataForm.county = data.basisData.county
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isLoading = true
            this.$http({
              url: this.$http.adornUrl(`/basisdata/basisdata/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createUserName': this.dataForm.createUserName,
                'articleType': this.dataForm.articleType,
                'articleTitle': this.dataForm.articleTitle,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'articleContent': this.dataForm.articleContent,
                'isDelete': this.dataForm.isDelete,
                'county': this.dataForm.county
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
